package com.lee.dynamic.config;

public final class DataSourceHolder
{
    public static final ThreadLocal<DataSourceType> HOLDER = new ThreadLocal<>();

    /**
     * 绑定当前线程数据源
     * @param type
     */
    public static void putDataSourceType(DataSourceType type)
    {
        if (type == null) {
            throw new NullPointerException();
        }
        HOLDER.set(type);
    }

    /**
     * 获取当前线程的数据源
     * @return
     */
    public static DataSourceType getDataSourceType()
    {
        return  HOLDER.get() == null ? DataSourceType.SLAVE : HOLDER.get();
    }

    /**
     *
     */
    public static void clearDataSourceType() {
        HOLDER.remove();
    }
}